package haoyou.csnf.manager.module.pm.service.rulesclasshour;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import haoyou.csnf.manager.module.pm.controller.admin.rulesclasshour.vo.*;
import haoyou.csnf.manager.module.pm.dal.dataobject.rulesclasshour.RulesClasshourDO;
import haoyou.csnf.manager.framework.common.pojo.PageResult;
import haoyou.csnf.manager.framework.common.pojo.PageParam;
import haoyou.csnf.manager.framework.common.util.object.BeanUtils;

import haoyou.csnf.manager.module.pm.dal.mysql.rulesclasshour.RulesClasshourMapper;

import static haoyou.csnf.manager.framework.common.exception.util.ServiceExceptionUtil.exception;
import static haoyou.csnf.manager.framework.common.util.collection.CollectionUtils.convertList;
import static haoyou.csnf.manager.framework.common.util.collection.CollectionUtils.diffList;
import static haoyou.csnf.manager.module.pm.enums.ErrorCodeConstants.*;

/**
 * 课时费规则 Service 实现类
 *
 * @author 好有经验源码
 */
@Service
@Validated
public class RulesClasshourServiceImpl implements RulesClasshourService {

    @Resource
    private RulesClasshourMapper rulesClasshourMapper;

    @Override
    public Integer createRulesClasshour(RulesClasshourSaveReqVO createReqVO) {
        // 插入
        RulesClasshourDO rulesClasshour = BeanUtils.toBean(createReqVO, RulesClasshourDO.class);
        rulesClasshourMapper.insert(rulesClasshour);
        // 返回
        return rulesClasshour.getId();
    }

    @Override
    public void updateRulesClasshour(RulesClasshourSaveReqVO updateReqVO) {
        // 校验存在
        validateRulesClasshourExists(updateReqVO.getId());
        // 更新
        RulesClasshourDO updateObj = BeanUtils.toBean(updateReqVO, RulesClasshourDO.class);
        rulesClasshourMapper.updateById(updateObj);
    }

    @Override
    public void deleteRulesClasshour(Integer id) {
        // 校验存在
        validateRulesClasshourExists(id);
        // 删除
        rulesClasshourMapper.deleteById(id);
    }

    @Override
        public void deleteRulesClasshourListByIds(List<Integer> ids) {
        // 校验存在
        validateRulesClasshourExists(ids);
        // 删除
        rulesClasshourMapper.deleteByIds(ids);
        }

    private void validateRulesClasshourExists(List<Integer> ids) {
        List<RulesClasshourDO> list = rulesClasshourMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(RULES_CLASSHOUR_NOT_EXISTS);
        }
    }

    private void validateRulesClasshourExists(Integer id) {
        if (rulesClasshourMapper.selectById(id) == null) {
            throw exception(RULES_CLASSHOUR_NOT_EXISTS);
        }
    }

    @Override
    public RulesClasshourDO getRulesClasshour(Integer id) {
        return rulesClasshourMapper.selectById(id);
    }

    @Override
    public PageResult<RulesClasshourDO> getRulesClasshourPage(RulesClasshourPageReqVO pageReqVO) {
        return rulesClasshourMapper.selectPage(pageReqVO);
    }

}